/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import java.util.HashMap;
import java.util.Map;

import l2s.commons.util.Rnd;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.scripting.ScriptFile;

public class Q327_ReclaimTheLand extends Quest implements ScriptFile
{
	private static final String qn = "327_ReclaimTheLand";
	private static final String descr = "Recover the Farmland";
	
	// NPCs
	private static int Piotur = 30597;
	private static int Iris = 30034;
	private static int Asha = 30313;
	
	// Quest Items
	private static int TUREK_DOGTAG = 1846;
	private static int TUREK_MEDALLION = 1847;
	private static int CLAY_URN_FRAGMENT = 1848;
	private static int BRASS_TRINKET_PIECE = 1849;
	private static int BRONZE_MIRROR_PIECE = 1850;
	private static int JADE_NECKLACE_BEAD = 1851;
	private static int ANCIENT_CLAY_URN = 1852;
	private static int ANCIENT_BRASS_TIARA = 1853;
	private static int ANCIENT_BRONZE_MIRROR = 1854;
	private static int ANCIENT_JADE_NECKLACE = 1855;
	
	private static Map<Integer, int[]> DROPLIST = new HashMap<>();
	private static Map<Integer, Integer> EXP = new HashMap<>();
	
	public Q327_ReclaimTheLand(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Piotur);
		addTalkId(Iris);
		addTalkId(Asha);
		
		DROPLIST.put(20501, new int[]
		{
			TUREK_MEDALLION,
			12
		});
		DROPLIST.put(20500, new int[]
		{
			TUREK_DOGTAG,
			7
		});
		DROPLIST.put(20499, new int[]
		{
			TUREK_DOGTAG,
			8
		});
		DROPLIST.put(20498, new int[]
		{
			TUREK_DOGTAG,
			10
		});
		DROPLIST.put(20497, new int[]
		{
			TUREK_MEDALLION,
			11
		});
		DROPLIST.put(20496, new int[]
		{
			TUREK_DOGTAG,
			9
		});
		DROPLIST.put(20495, new int[]
		{
			TUREK_MEDALLION,
			13
		});
		
		EXP.put(ANCIENT_CLAY_URN, 913);
		EXP.put(ANCIENT_BRASS_TIARA, 1065);
		EXP.put(ANCIENT_BRONZE_MIRROR, 1065);
		EXP.put(ANCIENT_JADE_NECKLACE, 1294);
		
		for (int kill_id : DROPLIST.keySet())
		{
			addKillId(kill_id);
		}
		
		questItemIds = new int[]
		{
			TUREK_DOGTAG,
			TUREK_MEDALLION
		};
		
	}
	
	private static boolean ExpReward(QuestState st, int item_id)
	{
		Integer exp = EXP.get(item_id);
		if (exp == null)
		{
			exp = 182;
		}
		int exp_reward = st.getQuestItemsCount(item_id * exp);
		if (exp_reward == 0)
		{
			return false;
		}
		st.takeItems(item_id, -1);
		st.rewardExpAndSp(exp_reward, 0);
		st.playSound(QuestState.SOUND_MIDDLE);
		return true;
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		
		QuestState st = player.getQuestState(qn);
		if (st == null)
		{
			return null;
		}
		int _state = st.getState();
		if (event.equalsIgnoreCase("piotur_q0327_03.htm") && (_state == STATE_CREATED))
		{
			st.setState(STATE_STARTED);
			st.set("cond", "1");
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("piotur_q0327_06.htm") && (_state == STATE_STARTED))
		{
			st.playSound(QuestState.SOUND_FINISH);
			st.exitQuest(true);
		}
		else if (event.equalsIgnoreCase("trader_acellopy_q0327_02.htm") && (_state == STATE_STARTED) && (st.getQuestItemsCount(CLAY_URN_FRAGMENT) >= 5))
		{
			st.takeItems(CLAY_URN_FRAGMENT, 5);
			if (!Rnd.chance(80))
			{
				return "trader_acellopy_q0327_10.htm";
			}
			st.giveItems(ANCIENT_CLAY_URN, 1);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "trader_acellopy_q0327_03.htm";
		}
		else if (event.equalsIgnoreCase("trader_acellopy_q0327_04.htm") && (_state == STATE_STARTED) && (st.getQuestItemsCount(BRASS_TRINKET_PIECE) >= 5))
		{
			st.takeItems(BRASS_TRINKET_PIECE, 5);
			if (!Rnd.chance(80))
			{
				return "trader_acellopy_q0327_10.htm";
			}
			st.giveItems(ANCIENT_BRASS_TIARA, 1);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "trader_acellopy_q0327_05.htm";
		}
		else if (event.equalsIgnoreCase("trader_acellopy_q0327_06.htm") && (_state == STATE_STARTED) && (st.getQuestItemsCount(BRONZE_MIRROR_PIECE) >= 5))
		{
			st.takeItems(BRONZE_MIRROR_PIECE, 5);
			if (!Rnd.chance(80))
			{
				return "trader_acellopy_q0327_10.htm";
			}
			st.giveItems(ANCIENT_BRONZE_MIRROR, 1);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "trader_acellopy_q0327_07.htm";
		}
		else if (event.equalsIgnoreCase("trader_acellopy_q0327_08.htm") && (_state == STATE_STARTED) && (st.getQuestItemsCount(JADE_NECKLACE_BEAD) >= 5))
		{
			st.takeItems(JADE_NECKLACE_BEAD, 5);
			if (!Rnd.chance(80))
			{
				return "trader_acellopy_q0327_09.htm";
			}
			st.giveItems(ANCIENT_JADE_NECKLACE, 1);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "trader_acellopy_q0327_07.htm";
		}
		else if (event.equalsIgnoreCase("iris_q0327_03.htm") && (_state == STATE_STARTED))
		{
			if (!ExpReward(st, CLAY_URN_FRAGMENT))
			{
				return "iris_q0327_02.htm";
			}
		}
		else if (event.equalsIgnoreCase("iris_q0327_04.htm") && (_state == STATE_STARTED))
		{
			if (!ExpReward(st, BRASS_TRINKET_PIECE))
			{
				return "iris_q0327_02.htm";
			}
		}
		else if (event.equalsIgnoreCase("iris_q0327_05.htm") && (_state == STATE_STARTED))
		{
			if (!ExpReward(st, BRONZE_MIRROR_PIECE))
			{
				return "iris_q0327_02.htm";
			}
		}
		else if (event.equalsIgnoreCase("iris_q0327_06.htm") && (_state == STATE_STARTED))
		{
			if (!ExpReward(st, JADE_NECKLACE_BEAD))
			{
				return "iris_q0327_02.htm";
			}
		}
		else if (event.equalsIgnoreCase("iris_q0327_07.htm") && (_state == STATE_STARTED))
		{
			if (!(ExpReward(st, ANCIENT_CLAY_URN) || ExpReward(st, ANCIENT_BRASS_TIARA) || ExpReward(st, ANCIENT_BRONZE_MIRROR) || ExpReward(st, ANCIENT_JADE_NECKLACE)))
			{
				return "iris_q0327_02.htm";
			}
		}
		
		return event;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
		{
			return null;
		}
		int _state = st.getState();
		int npcId = npc.getNpcId();
		if (_state == STATE_CREATED)
		{
			if (npcId != Piotur)
			{
				return getNoQuestMsg();
			}
			if ((st.getPlayer().getLevel() < 25) || (player.getLevel() > 34))
			{
				st.exitQuest(true);
				return "piotur_q0327_01.htm";
			}
			st.set("cond", "0");
			return "piotur_q0327_02.htm";
		}
		
		if (_state != STATE_STARTED)
		{
			return getNoQuestMsg();
		}
		
		if (npcId == Piotur)
		{
			int reward = (st.getQuestItemsCount(TUREK_DOGTAG) * 40) + (st.getQuestItemsCount(TUREK_MEDALLION) * 50);
			if (reward == 0)
			{
				return "piotur_q0327_04.htm";
			}
			st.takeItems(TUREK_DOGTAG, -1);
			st.takeItems(TUREK_MEDALLION, -1);
			st.giveItems(57, reward);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "piotur_q0327_05.htm";
		}
		if (npcId == Iris)
		{
			return "iris_q0327_01.htm";
		}
		if (npcId == Asha)
		{
			return "trader_acellopy_q0327_01.htm";
		}
		
		return getNoQuestMsg();
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
		{
			return null;
		}
		if (st.getState() != STATE_STARTED)
		{
			return null;
		}
		
		int npcId = npc.getNpcId();
		
		int[] mobDrop = DROPLIST.get(npcId);
		if (mobDrop == null)
		{
			return null;
		}
		
		if (Rnd.chance(mobDrop[1]))
		{
			int n = Rnd.get(100);
			if (n < 25)
			{
				st.giveItems(CLAY_URN_FRAGMENT, 1);
			}
			else if (n < 50)
			{
				st.giveItems(BRASS_TRINKET_PIECE, 1);
			}
			else if (n < 75)
			{
				st.giveItems(BRONZE_MIRROR_PIECE, 1);
			}
			else
			{
				st.giveItems(JADE_NECKLACE_BEAD, 1);
			}
		}
		st.giveItems(mobDrop[0], 1);
		st.playSound(QuestState.SOUND_ITEMGET);
		
		return null;
	}
	
	public static void onLoad()
	{
		new Q327_ReclaimTheLand(327, qn, descr);
	}
}